// 该文件专门用于创建整个应用的路由器
import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

// 引入路由组件
import myHome from "@/pages/Home/myHome.vue";
import hotSpot from "@/pages/hotSpot/hotSpot.vue";
import myLogin from "@/pages/Login/myLogin.vue";
import myMine from "@/pages/Mine/myMine.vue";
import myRegistered from "@/pages/Registered/myRegistered.vue";
import myForget from "@/pages/Forget/myForget.vue";
// 我的消息
import messAge from "@/pages/Mine/messAge/messAge";
// 我的评论
import comMent from "@/pages/Mine/comMent/comMent";
// 我的点赞
import giVe from "@/pages/Mine/giVe/giVe";
// 我的收藏
import colLect from "@/pages/Mine/colLect/colLect";
import hotSpotDetail from "@/pages/hotSpot/hotSpotDetail"

// 配置路由
const router = new VueRouter({
  routes: [{ 
      path: "*",
      redirect: "/home",
      meta: {
        show: true
      },
    },
    {
      path: "/home",
      component: myHome,
      meta: {
        show: true
      },
    },
    {
      path: "/hotSpot",
      component: hotSpot,
      meta: {
        show: true
      },
      
    },
    {    
      path: "/hotSpotDetail",
      component: hotSpotDetail,
      meta: {
        show: false
      },
    },
    {
      path: "/login",
      component: myLogin,
      meta: {
        show: false
      },
    },
    {
      path: "/mine",
      component: myMine,
      meta: {
        show: true
      },
      children: [{
        path: "messAge",
        component: messAge,
        meta: {
          show: true
        },
      }, {
        path: "comMent",
        component: comMent,
        meta: {
          show: true
        },
      }, {
        path: "giVe",
        component: giVe,
        meta: {
          show: true
        },
      }, {
        path: "colLect",
        component: colLect,
        meta: {
          show: true
        },
      }, {
        path: "/mine",
        redirect: '/mine/messAge',
        meta: {
          show: true
        },
      }]
    },
    {
      path: "/registered",
      component: myRegistered,
      meta: {
        show: false
      },
    },
    {
      path: "/forget",
      component: myForget,
      meta: {
        show: false
      },
    },
  ],
});
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token");
  if (token) {
    if (to.path == '/login') {
      next("/home");

    } else {
      next();
    }
  } else {
    next();

  }
});
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router;